﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MShop.Core.Domain
{
    [MetadataType(typeof(AreaGroup_Validation))]
    public partial class AreaGroup:INode<AreaGroup>
    {

        public AreaGroup()
        {
            this.Created = DateTime.Now;
            this.Modified = DateTime.Now;
        }

        public string TableRowClass
        {
            get
            {
                if (this.ParentId.HasValue)
                {
                    return string.Concat("child-of-node-", this.AreaGroup2.NodeId);
                }
                else
                    return null;
            }
        }

        public string TableRowId
        {
            get
            {
                return string.Concat("node-", NodeId);
            }
        }
        int? INode.ParentId
        {
            get { return this.ParentId; }
        }
        public bool IsRoot
        {
            get { return !this.ParentId.HasValue; }
        }

        public int Depth
        {
            get
            {
                int i = 0;
                var p = this.AreaGroup2;
                while (p != null)
                {
                    p = p.AreaGroup2;
                    i++;
                }
                return i;
            }
        }

        public int NodeId { get; set; }



        public bool HasChild
        {
            get
            {
                return this.AreaGroup1.Count > 0;
            }
        }


        public string CurrentAreaName
        {
            get
            {   
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                List<string> list=new List<string>();
                var p = this.AreaGroup2;
                while (p != null)
                {
                    list.Add(p.Name);
                    p = p.AreaGroup2;
                }

                list.Reverse();
                foreach (var s in list)
                {
                    sb.Append(s);
                    sb.Append("-");
                }
                sb.Append(Name);
                return sb.ToString();
            }
        }


        public AreaGroup Parent
        {
            get { return this.AreaGroup2; }
        }


        public object Key
        {
            get { return this.Id; }
        }


        public IEnumerable<AreaGroup> ChildNodes
        {
            get 
            {
                return this.AreaGroup1;
            }
        }

      
    }
   

    internal sealed class AreaGroup_Validation
    {
        [DisplayName("上级地区")]
        public int ParentId { get; set; }

        [DisplayName("名称")]
        public int Name { get; set; }

        [DisplayName("邮政编码")]
        public string Zip { get; set; }

        [DisplayName("地区名称")]
        public string AreaName { get; set; }
      
    }
}